<?php
General::getTpl()->assign('extraTitle',"Kommentare");

if(!isset($_GET['conId']) || !is_numeric($_GET['conId'])) exit;
if(!isset($_GET['conPage'])) exit;

General::getTpl()->assign('conId',$_GET['conId']);
General::getTpl()->assign('conPage',$_GET['conPage']);

if(!Admin::checkPermission('canManageComments')) {
	General::ouputMessage('Fehler','Sie haben nicht gen&uuml;gend Rechte um diese Seite zu betrachten');
}else{
	/**
	 * EDIT Comment
	 */
	if(isset($_GET['do']) && $_GET['do'] == "edit" && isset($_GET['id']) && is_numeric($_GET['id'])) {
		// Get Data
		$query = DB::getInstance()->query("
			SELECT
				comTitle,
				comText,
				comAuthor,
				comAuthorEmail,
				comDate
			FROM
				".Tables::$commentsTbl."
			WHERE
				id = '".DB::parseVar($_GET['id'])."'");
		$data = DB::getInstance()->fetchAssoc($query);
		
		// Form-Data
		$formField = array();
		$i = 0;
		
		$formField[$i]['fieldName'] = "Titel";
		$formField[$i]['fieldType'] = Form::$TYPE_TEXT;
		$formField[$i]['fieldDefaultValue'] = $data['comTitle'];
		$formField[$i]['fieldCheck'] = true;
		$formField[$i]['fieldCheckExtra'] = Form::$CHECK_TEXT_NOTEMPTY;
		$i++;
		
		$formField[$i]['fieldName'] = "Text";
		$formField[$i]['formName'] = "text";
		$formField[$i]['fieldDisplayBBCodeSmilies'] = true;
		$formField[$i]['fieldSelectOptions'] = "50_10";
		$formField[$i]['fieldType'] = Form::$TYPE_TEXTAREA;
		$formField[$i]['fieldDefaultValue'] = htmlspecialchars_decode($data['comText']);
		$formField[$i]['fieldCheck'] = true;
		$formField[$i]['fieldCheckExtra'] = Form::$CHECK_TEXTAREA_NOTEMPTY;
		$i++;
	
		$formField[$i]['fieldName'] = "Name";
		$formField[$i]['fieldDescription'] = "Bitte geben Sie ihren Namen ein.";
		$formField[$i]['fieldType'] = Form::$TYPE_TEXT;
		$formField[$i]['fieldDefaultValue'] = $data['comAuthor'];
		$formField[$i]['fieldCheck'] = true;
		$formField[$i]['fieldCheckExtra'] = Form::$CHECK_TEXT_NOTEMPTY;
		$i++;
		
		$formField[$i]['fieldName'] = "Email";
		$formField[$i]['fieldDescription'] = "Ihre Email-Adresse";
		$formField[$i]['fieldType'] = Form::$TYPE_TEXT;
		$formField[$i]['fieldDefaultValue'] = $data['comAuthorEmail'];
		$formField[$i]['fieldCheck'] = false;
		$i++;
		
		$form = new Form();
		$form->initByData("Kommentar bearbeiten",true,"Absenden!",$formField);

		// Done?
		if($form->completeCheck()) {

			// Save Comment
			DB::getInstance()->query("
				UPDATE
					".Tables::$commentsTbl."
				SET
					comTitle = '".DB::parseVar(htmlspecialchars($_POST['Titel']))."',
					comText = '".DB::parseVar(htmlspecialchars($_POST['text']))."',
					comAuthor = '".DB::parseVar($_POST['Name'])."',
					comAuthorEmail = '".DB::parseVar($_POST['Email'])."'
				WHERE
					id = '".$_GET['id']."'");

			// Save Data
			General::ouputMessage('Erfolgreich','Der Kommentar wurden erfolgreich bearbeitet.');
		}else{
			$form->outputFormular('form','formular.html');
		}
	}
	
	

	
	if(isset($_GET['do']) && $_GET['do'] == "del" && isset($_GET['id']) && is_numeric($_GET['id'])) {
		require_once './kernel/formCheck.php';
		$form = new FormCheck("Bist du sicher?","L&ouml;schen");
		// Done?
		if($form->completeCheck()) {
			if($form->isChecked()) {
				// Delete Comment
				DB::getInstance()->query("
					DELETE FROM
						".Tables::$commentsTbl."
					WHERE
						id = '".$_GET['id']."'");
			}
		}else{
			$form->outputFormular('form','formular.html');
		}
	}
	
	
	
	/**
	 * Output Comments
	 */
	$elementsPerPage = 10;
	
	$_GET['pages'] = (isset($_GET['pages']) && is_numeric($_GET['pages'])) ? $_GET['pages']:1;
    $min = General::pages_begin($_GET['pages'],$elementsPerPage);
    
    
    // Error-Count and Kinds
    $allComments = Comments::getCommentsMin($_GET['conId'],$_GET['conPage']);
    
    // Get Page-Output
    $pages = General::pages_end(count($allComments),$elementsPerPage,$_GET['pages']);
	General::getTpl()->assign('pages',$pages);
	
	
	$comments = Comments::getCommentsMin($_GET['conId'],$_GET['conPage'],$min,$elementsPerPage);
	General::getTpl()->assign('comments',$comments);
	
	General::getTpl()->assign('body',
		General::getTpl()->fetch('../admin/comments.html'));
}
?>